package com.gt.quality.config;


import cn.dev33.satoken.exception.NotLoginException;
import com.gt.quality.base.BusinessSelfException;
import com.gt.quality.base.pojo.Resp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLSyntaxErrorException;

/**
 * @description: 全局异常处理：
 * @author: makeJava
 * @since JDK 21
 **/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    // 全局异常拦截
    @ExceptionHandler
    public Resp<String> handlerException(Exception e) {
        log.error("服务执行异常---->{}", e.getMessage(), e);
        return Resp.error("系统繁忙,请稍后再试");
    }

    // 全局异常拦截
    @ExceptionHandler(value = BusinessSelfException.class)
    public Resp<String> handlerException(BusinessSelfException e) {
        log.error("业务处理---->{}", e.getMessage(), e);
        return Resp.error(e.getMessage());
    }

    // 全局异常拦截
    @ExceptionHandler(value = NotLoginException.class)
    public Resp<String> handlerException(NotLoginException e) {
        log.info(e.getMessage());
        return Resp.respNotLogin();
    }


    @ExceptionHandler(value = {SQLException.class, SQLSyntaxErrorException.class})
    public Resp<String> mySqlExecuteError(SQLException e) {
        log.error("Mysql执行异常{}", e.getMessage(), e);
        return Resp.error("Mysql执行异常,请联系管理员");
    }


    @ExceptionHandler(value = HttpMessageNotReadableException.class)
    public Resp<String> msgNotFind(HttpMessageNotReadableException e) {
        log.error("请求错误{}", e.getMessage(), e);
        return Resp.error("非法操作...");
    }
}
